<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author: zl

// +----------------------------------------------------------------------



namespace app\crm\controller;



use app\admin\controller\ApiCommon;

use app\crm\model\BalanceLog;

use app\crm\model\Setting;

use app\crm\model\SettingCategory;

use app\crm\model\Service;

use app\crm\model\ReturnVist;

use app\crm\model\ReturnVistT;

use app\crm\model\Customer as CustomerModel;

use app\crm\model\TransferShop;

use app\crm\model\CustomerConfig as CustomerConfigModel;

use app\crm\model\CustomerGrade as CustomerGradeModel;

use think\response\Json;

use think\facade\Db;

use think\facade\Request;

use app\admin\model\Coupon as CouponModel;

use app\common\traits\ActionRecordTrait;


class Preparevip extends ApiCommon

{
    protected $customerModel;

    protected $customerConfigModel;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @other 其他根据系统设置

     **/

    public function initialize()

    {

        $action = [

            'permission' => ['daochu_yubeihuiyuan'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a = strtolower($request->action());



        $this->customerModel = new CustomerModel();

        $this->customerConfigModel = new CustomerConfigModel();

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    public function daochu_yubeihuiyuan()
    {
        echo "导出预备会员";
        $where = [];
        array_push($where,['member_classification','in',[71,72,73]]);
        // array_push($where,['membership_leve','=',0]);

        $customer_list = Db::name('customer')->where($where)->column('id,customer_name,telephone,cj_time,shop_id,consumption,nurse_id');

        // echo "<pre>";
        // print_r($customer_list);
        // echo "</pre>";

        $shop_ids = array_values(array_unique(array_column($customer_list,'shop_id')));

		$nurse_id = array_values(array_unique(array_column($customer_list,'nurse_id')));

        $where = [];

        array_push($where,['shop_id','in',$shop_ids]);

        $shopList = Db::name('shop')->where($where)->column('shop_name','shop_id');

        $where = [];

        array_push($where,['shop_id','in',$shop_ids]);

        $marketList = Db::name('shop')->where($where)->column('market_name','shop_id');


        $where = [];

        array_push($where,['id','in',$nurse_id]);

        $nurseList = Db::name('shop_person')->where($where)->column('name','id');
        // exit;

        $list = [];
        foreach($customer_list as $key => $val){
            $one = [];

            $one['customer_name'] = $val['customer_name'];

            $one['telephone'] = $val['telephone'];

            $one['market_name'] = $marketList[$val['shop_id']] ?? '';

            $one['shop_name'] = $shopList[$val['shop_id']] ?? '';

            $one['nurse_name'] = $nurseList[$val['nurse_id']] ?? '';

            $one['cj_time'] = $val['cj_time'] > 0 ? date('Y-m-d H:i:s',$val['cj_time']) : '';

            $one['consumption'] = $val['consumption'];

            array_push($list,$one);
        }

        // echo "<pre>";
        // print_r($list);
        
        $callName = [

            'customer_name'   =>'姓名',

            'telephone' =>'手机',

            'market_name' =>'市场',

            'shop_name' =>'门店',

            'nurse_name' =>'护理师',

            'cj_time'     =>'成交时间',

            'consumption'   =>'累计消费',

        ];

        $downUrl = exportExcelData('预备会员导出',$callName,$list,'xlsx',1);

        echo "<script language='javascript'>";

        echo " location='".$downUrl."';";

        echo "</script>"; 
        
    }

}